<?php
/**
 * Print recordset
 *
 * @author Arkadiusz Bisaga <abisaga@telaxus.com>
 * @copyright Copyright &copy; 2006, Telaxus LLC
 * @version 1.0
 * @license MIT
 * @package epesi-utils
 * @subpackage RecordBrowser
 */
if(!isset($_REQUEST['cid']) || !isset($_REQUEST['key'])) die('Invalid usage - missing param');
$cid = $_REQUEST['cid'];
$key = $_REQUEST['key'];

define('CID', $cid);
define('READ_ONLY_SESSION',true);
require_once('../../../include.php');

ModuleManager::load_modules();
set_time_limit(0);

$path = $_SESSION[$key]['path'];
$start_date = Module::static_get_module_variable($path, 'bang_ke_nop_tien_lap_dat_start_date', null);
$start_date = date('Y-m-d', strtotime($start_date));
$end_date = Module::static_get_module_variable($path, 'bang_ke_nop_tien_lap_dat_end_date', null);
$end_date = date('Y-m-d', strtotime($end_date));
$employee = Module::static_get_module_variable($path, 'bang_ke_nop_tien_lap_dat_truong_khu_vuc');

if(!empty($employee)){
    $tcpdf = Libs_TCPDFCommon::new_pdf('L');
    Libs_TCPDFCommon::SetFont($tcpdf, Libs_TCPDFCommon::$default_font, '', 13);
    Libs_TCPDFCommon::add_page($tcpdf);
}
$current_user = CRM_Contactscommon::get_my_record();
$list_area = SCTV_AreaCommon::get_area_by_id_manager($current_user['id']);
$room_name = isset($list_area[0]) ? $list_area[0]['name'] : '';

$html = '<table><tr><td align="center">CHI NHÁNH CÔNG TY SCTV TẠI QUẢNG NINH<br>PHÒNG GIAO DỊCH ' . mb_convert_case($room_name,MB_CASE_UPPER,'UTF-8') .'</td><td align="center"></td></tr></table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

if($start_date == $end_date)
    $ngay_thang_nam = 'Ngày ' . date('d/m/Y', strtotime($start_date));
else
    $ngay_thang_nam = 'Từ ngày ' . date('d/m/Y', strtotime($start_date)) . ' đến ngày ' . date('d/m/Y', strtotime($end_date));

$html = '<table><tr><td align="center"><h2>BẢNG KÊ NỘP TIỀN LẮP ĐẶT</h2></td></tr><tr><td align="center">' . $ngay_thang_nam . '</td></tr><tr><td align="center"></td></tr></table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

$manager_name = CRM_ContactsCommon::contact_format_no_company($employee);

$html = '<table width="100%" cellspacing="0" cellpadding="2" border="1" style="font-size:9">';

$html .= '<tr>';
$html .= '<td width="4%" align="center" style="font-weight:bold">STT</td>';
$html .= '<td width="14%" align="center" style="font-weight:bold">Nhân viên lắp đặt</td>';
$html .= '<td width="5%" align="center" style="font-weight:bold">Số phiếu thu</td>';
$html .= '<td width="6%" align="center" style="font-weight:bold">Số hợp đồng THC</td>';
$html .= '<td width="6%" align="center" style="font-weight:bold">Số hợp đồng Internet</td>';
$html .= '<td width="8%" align="center" style="font-weight:bold">Ngày nghiệm thu</td>';
$html .= '<td width="18%" align="center" style="font-weight:bold">Tên khách hàng</td>';
$html .= '<td width="8%" align="center" style="font-weight:bold">Phí hòa mạng</td>';
$html .= '<td width="7%" align="center" style="font-weight:bold">Bán đầu số</td>';
$html .= '<td width="8%" align="center" style="font-weight:bold">Bán vật tư</td>';
$html .= '<td width="8%" align="center" style="font-weight:bold">Ký quỹ</td>';
$html .= '<td width="9%" align="center" style="font-weight:bold">Tổng cộng</td>';
$html .= '</tr>';

$records = DB::GetAll('select * from sctv_bang_ke_nop_tien_lap_dat(%D, %D, %d)', array($start_date, $end_date, $employee));

$stt = 0;
$nv_phi_hoa_mang = 0;
$nv_tien_dau_so = 0;
$nv_tien_vat_tu = 0;
$nv_ky_quy = 0;
$nv_tong_cong = 0;
$tong_phi_hoa_mang = 0;
$tong_tien_dau_so = 0;
$tong_tien_vat_tu = 0;
$tong_ky_quy = 0;
$tong_tong_cong = 0;
$prev_name = null;
foreach($records as $key=>$record){
    $stt++;
    $employee_name = $record['employee_name'];
    $phi_hoa_mang = $record['phi_hoa_mang'];
    $tien_dau_so = $record['tien_dau_so'];
    $tien_vat_tu = $record['tien_vat_tu'];
    $ky_quy = $record['ky_quy'];
    $tong_cong = $record['tong_cong'];

    $tong_phi_hoa_mang += $phi_hoa_mang;
    $tong_tien_dau_so += $tien_dau_so;
    $tong_tien_vat_tu += $tien_vat_tu;
    $tong_ky_quy += $ky_quy;
    $tong_tong_cong += $tong_cong;

    if($prev_name == null || $prev_name <> $employee_name){
        if($prev_name != null){
            $html .= '<tr>';
            $html .= '<td align="center"></td>';
            $html .= '<td></td>';
            $html .= '<td align="center"></td>';
            $html .= '<td align="center"></td>';
            $html .= '<td align="center"></td>';
            $html .= '<td align="center"></td>';
            $html .= '<td><strong>TỔNG</strong></td>';
            $html .= '<td align="right"><strong>' . number_format($nv_phi_hoa_mang, 0, ',', '.') . '</strong></td>';
            $html .= '<td align="right"><strong>' . number_format($nv_tien_dau_so, 0, ',', '.') . '</strong></td>';
            $html .= '<td align="right"><strong>' . number_format($nv_tien_vat_tu, 0, ',', '.') . '</strong></td>';
            $html .= '<td align="right"><strong>' . number_format($nv_ky_quy, 0, ',', '.') . '</strong></td>';
            $html .= '<td align="right"><strong>' . number_format($nv_tong_cong, 0, ',', '.') . '</strong></td>';
            $html .= '</tr>';
        }
        $stt = 1;
        $prev_name = $employee_name;
        $nv_phi_hoa_mang = $phi_hoa_mang;
        $nv_tien_dau_so = $tien_dau_so;
        $nv_tien_vat_tu = $tien_vat_tu;
        $nv_ky_quy = $ky_quy;
        $nv_tong_cong = $tong_cong;
    }
    else{
        $nv_phi_hoa_mang += $phi_hoa_mang;
        $nv_tien_dau_so += $tien_dau_so;
        $nv_tien_vat_tu += $tien_vat_tu;
        $nv_ky_quy += $ky_quy;
        $nv_tong_cong += $tong_cong;
    }

    $html .= '<tr>';
    $html .= '<td align="center">' . $stt . '</td>';

    $html .= '<td>' . $employee_name . '</td>';
    $html .= '<td align="center">' . $record['receipt'] . '</td>';
    $html .= '<td align="center">' . $record['cable_contract_code'] . '</td>';
    $html .= '<td align="center">' . $record['internet_contract_code'] . '</td>';
    $html .= '<td align="center">' . date('d-m-Y', strtotime($record['acceptance_date'])) . '</td>';
    $html .= '<td>' . $record['customer_name'] . '</td>';
    $html .= '<td align="right">' . number_format($phi_hoa_mang, 0, ',', '.') . '</td>';
    $html .= '<td align="right">' . number_format($tien_dau_so, 0, ',', '.') . '</td>';
    $html .= '<td align="right">' . number_format($tien_vat_tu, 0, ',', '.') . '</td>';
    $html .= '<td align="right">' . number_format($ky_quy, 0, ',', '.') . '</td>';
    $html .= '<td align="right">' . number_format($tong_cong, 0, ',', '.') . '</td>';
    $html .= '</tr>';
}

if($prev_name != null){
    $html .= '<tr>';
    $html .= '<td align="center"></td>';
    $html .= '<td></td>';
    $html .= '<td align="center"></td>';
    $html .= '<td align="center"></td>';
    $html .= '<td align="center"></td>';
    $html .= '<td align="center"></td>';
    $html .= '<td><strong>TỔNG</strong></td>';
    $html .= '<td align="right"><strong>' . number_format($nv_phi_hoa_mang, 0, ',', '.') . '</strong></td>';
    $html .= '<td align="right"><strong>' . number_format($nv_tien_dau_so, 0, ',', '.') . '</strong></td>';
    $html .= '<td align="right"><strong>' . number_format($nv_tien_vat_tu, 0, ',', '.') . '</strong></td>';
    $html .= '<td align="right"><strong>' . number_format($nv_ky_quy, 0, ',', '.') . '</strong></td>';
    $html .= '<td align="right"><strong>' . number_format($nv_tong_cong, 0, ',', '.') . '</strong></td>';
    $html .= '</tr>';
}

$html .= '<tr>';
$html .= '<td align="center"></td>';
$html .= '<td></td>';
$html .= '<td align="center"></td>';
$html .= '<td align="center"></td>';
$html .= '<td align="center"></td>';
$html .= '<td align="center"></td>';
$html .= '<td><strong>TỔNG CỘNG</strong></td>';
$html .= '<td align="right"><strong>' . number_format($tong_phi_hoa_mang, 0, ',', '.') . '</strong></td>';
$html .= '<td align="right"><strong>' . number_format($tong_tien_dau_so, 0, ',', '.') . '</strong></td>';
$html .= '<td align="right"><strong>' . number_format($tong_tien_vat_tu, 0, ',', '.') . '</strong></td>';
$html .= '<td align="right"><strong>' . number_format($tong_ky_quy, 0, ',', '.') . '</strong></td>';
$html .= '<td align="right"><strong>' . number_format($tong_tong_cong, 0, ',', '.') . '</strong></td>';
$html .= '</tr>';

$html .= '</table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

$html = '<table style="padding-top: 20px"><tr>';
$html .= '<td>Người nộp tiền</td>';
$html .= '<td>Kế toán vật tư</td>';
$html .= '<td>Kế toán tiền mặt</td>';
$html .= '</tr></table>';
Libs_TCPDFCommon::writeHTML($tcpdf, $html, false);

$buffer = Libs_TCPDFCommon::output($tcpdf);

header('Content-Type: application/pdf');
header('Content-Length: '.strlen($buffer));
header('Content-disposition: inline; filename="bang_ke_nop_tien_lap_dat_'.$id.'.pdf"');

print($buffer);

?>
